<?php

/**
 * Created by PhpStorm.
 * User: wenqiang.yq@gmail.com
 * Date: 16-2-29
 * Time: 下午9:55
 */
namespace ElasticSearch;

include_once dirname(__FILE__) . DIRECTORY_SEPARATOR . "core" . DIRECTORY_SEPARATOR . "EsHttp.php";
include_once dirname(__FILE__) . DIRECTORY_SEPARATOR . "core" . DIRECTORY_SEPARATOR . "EsCore.php";
include_once dirname(__FILE__) . DIRECTORY_SEPARATOR . "core" . DIRECTORY_SEPARATOR . "EsSearch.php";
include_once dirname(__FILE__) . DIRECTORY_SEPARATOR . "core" . DIRECTORY_SEPARATOR . "EsTools.php";
include_once dirname(__FILE__) . DIRECTORY_SEPARATOR . "core" . DIRECTORY_SEPARATOR . "EsDsl.php";
include_once dirname(__FILE__) . DIRECTORY_SEPARATOR . "core" . DIRECTORY_SEPARATOR . "EsIndex.php";
include_once dirname(__FILE__) . DIRECTORY_SEPARATOR . "core" . DIRECTORY_SEPARATOR . "EsDocument.php";

define("ES_JSON_FOLD", dirname(dirname(__FILE__)) . DIRECTORY_SEPARATOR . "search" . DIRECTORY_SEPARATOR);

class ElasticSearch
{
    public $_index = "";

    public $_hosts = "";

    public $_type = "";

    public function __construct($hosts = "", $index = "", $type = "")
    {
        $this->_hosts = $hosts;
        $this->_index = $index;
        $this->_type = $type;
    }

    /**
     * 翻译sql语句成es查询的dsl字符串
     * @return EsDsl
     */
    public function dsl()
    {
        $dsl = new EsDsl();
        return $dsl;
    }

    /**
     *
     * @param $hosts
     * @return EsIndex
     */
    public function index($hosts = "")
    {
        !$hosts && $hosts = $this->_hosts;
        $index = new EsIndex($hosts);
        return $index;
    }

    /**
     * @return EsData
     */
    public function data()
    {
        $data = new EsDocument($this->_hosts, $this->_index, $this->_type);
        return $data;
    }

    /**
     * @return EsBase
     */
    public function es()
    {
        $data = new EsBase($this->_hosts, $this->_index, $this->_type);
        return $data;
    }

    /**
     * @return EsSearch
     */
    public function search()
    {
        $search = new EsSearch($this->_hosts, $this->_index, $this->_type);
        return $search;
    }
}